//
//  YCLEmoticonToolBar.swift
//  02.表情键盘
//
//  Created by yaochunlin on 16/4/21.
//  Copyright © 2016年 yaochunlin. All rights reserved.
//

import UIKit

//按钮点击的协议
protocol YCLEmoticonToolBarDelegate:NSObjectProtocol {
    
    func emoticonToolBar(toolBar: YCLEmoticonToolBar, didSelectedType type: YCLEmoticonToolBarType)
}

//按钮类型
enum YCLEmoticonToolBarType: Int {
    
    case Recent = 0 // 最近
    case Default = 1 // 默认
    case Emoji = 2  // Emoji
    case Lxh = 3    // 浪小花
    
}

class YCLEmoticonToolBar: UIView {
    
    private var selectedBtn: UIButton?
    
    //代理属性
    weak var delegate: YCLEmoticonToolBarDelegate?

    //加载xib返回YCLEmoticonToolBar
    class func emoticonToolBar() -> YCLEmoticonToolBar {
        
        return NSBundle.mainBundle().loadNibNamed("EmoticonToolBar", owner: nil, options: nil).last as! YCLEmoticonToolBar
    }

    @IBAction func btnClick(sender: UIButton) {
        
        let type = YCLEmoticonToolBarType(rawValue: sender.tag)
        
        switchSelectedButton(sender)
        
        delegate?.emoticonToolBar(self, didSelectedType: type!)
    }
    
    //通过外界传入的组，确定按钮的位置
    func switchSelectedButtonWithSection(section: Int) {
        
        let btn = subviews[section] as! UIButton
        
        switchSelectedButton(btn)
        
    }
    
    //默认选中第一个按钮
    override func willMoveToSuperview(newSuperview: UIView?) {
        
        super.willMoveToSuperview(newSuperview)
        
        let btn = subviews[0] as! UIButton
        
        switchSelectedButton(btn)
        
    }
    
    //切换选中按钮
    private func switchSelectedButton(button: UIButton) {
        
        //若点击相同按钮，不作操作
        if selectedBtn == button {
            
            return
        }
    
        selectedBtn?.selected = false
        
        button.selected = true
        
        selectedBtn = button
        
        
    }
}
